package main

import (
	"net/http"
	"strings"

	"github.com/julienschmidt/httprouter"
)

//...
func MiddleAuth(router httprouter.Handle) httprouter.Handle {
	return httprouter.Handle(
		func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
			token := r.Header.Get("token")
			if strings.EqualFold(token, "tokenID") {
				router(w, r, p)
				w.Write([]byte("登录token获得"))
				return
			}
			w.WriteHeader(401)
			w.Write([]byte("401"))
		})
}

//...
func RegisterRouter() *httprouter.Router {
	router := httprouter.New()

	router.GET("/home/:id", MiddleAuth(home))

	return router
}

func home(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	// r.ParseForm()
	// name := r.PostForm.Get("name") // 获取它
	// fmt.Println(name)
	id := p.ByName("id")
	w.Write([]byte(id))
}

func main() {
	router := RegisterRouter()

	http.ListenAndServe(":8085", router)
}
